#{extends 'AdminM/frame.html'/}
#{set 'moreStyles'}
<link rel="stylesheet" type="text/css" media="screen" href="@{'/public/pager/Pager.css'}" />
<link rel="stylesheet" type="text/css" media="screen" href="@{'/public/stylesheets/table.scss'}" />
#{/set}
#{set 'moreScripts'}
<script src="@{'/public/pager/jquery.pager.js'}" type="text/javascript" charset="utf-8"></script>
#{/set}
#{set 'activeItem'}3#{/set}
<div class="navigationBar">
	<table>
		<tr>
			<td><img src="@{'/public/images/navigationHouse.gif'}"></td>
			<td><strong>当前位置：</strong></td>
			<td>
				<a href="/admin">首页</a>
				>>
				<a href="#">商品管理</a>
				>>
				<a href="/admin/commodities;admin">商品列表</a>
				>>
				<font class="current">编辑商品价格</font>
			</td>
		</tr>
	</table>
</div>

<script language="javascript">
$(function(){
	reloadTable();
	$("#dialog-form").dialog({
		resizable: false,
		height:200,
		modal: true,
		autoOpen: false,
		buttons: {
			'保存': function() {
				$.ajax({
					url: "@{admin.Commodities.updatePriceTable()}",
					data: getAttrStr(),
					type: "POST",
					dataType: "json",
					complete: function(XMLHttpRequest, textStatus, errorThrown) {
						if(textStatus == "error") {
							var errJson = eval("("+XMLHttpRequest.responseText+")");
							var errMsg = "操作失败，"+errJson.message;
							$("#errMsg").html(errMsg);
							alert(errMsg);
						}
						$("#dialog-form").dialog('close');
						reloadTable();
					}
				});
			},
			'取消': function() {
				$(this).dialog('close');
			}
		}
		
	});
});

function getAttrStr() {
	var editCommodityId = $("#editCommodityId").val();
	var groupId = $("#editGroupId").val();
	var price = $("#editPricedCommodityPrice").val();
	return "pricedCommodity.commodity.id=" + editCommodityId 
		+ "&pricedCommodity.group.id=" + groupId + "&pricedCommodity.price=" + price;
}

//重新载入listtable。
function reloadTable() {
	$("#errMsg").html("");
	var dataStr = getSearchCriteria();
	var id = $("#commodityId").val();
	$.ajax({
		url: "/admin/commodities/"+id+";editPriceTable",
		data: dataStr,
		type: "POST",
		dataType: "html",
		complete: function(XMLHttpRequest, textStatus, errorThrown) {
			if(textStatus == "error") {
				var errJson = eval("("+XMLHttpRequest.responseText+")");
				var errMsg = "操作失败，"+errJson.message;
				$("#errMsg").html(errMsg);
				alert(errMsg);
			} else {
				$("#tableDiv").html(XMLHttpRequest.responseText);
				initialPager();
			}
		}
	});
}

//组装查询条件与页码数据。
function getSearchCriteria(toPage) {
	var dataStr="";
	var elements = $(".queryBean");
	if(elements && elements.length!=0) {
		for(var i=0 ; i<elements.length ; i++) {
			if(dataStr) dataStr = dataStr+"&";
			if(elements[i].name != "queryBean.currentPage" || !toPage) {
				dataStr = dataStr + elements[i].name + "=" + $(elements[i]).val();
			}
		}
	}
	if(toPage) {
		if(dataStr) dataStr = dataStr+"&";
		dataStr = dataStr + "queryBean.currentPage=" + toPage;
	}
	return dataStr;
}

function newPriceOpen() {
	var id = $("#commodityId").val();
	var name = $("#commodityName").val();
	$("#editCommodityId").val(id);
	$("#editCommodityName").val(name);
	$("#editPricedCommodityPrice").val("");
	$("#dialog-form").dialog('open');
}

function editPriceOpen(id, name, groupId, price) {
	$("#editCommodityId").val(id);
	$("#editCommodityName").val(name);
	$("#editGroupId").val(groupId);
	$("#editPricedCommodityPrice").val(price);
	$("#dialog-form").dialog('open');
}

function updateStatus(id, status) {
	$.ajax({
		url: "/admin/commodities/pricestatus/"+id,
		data: "id="+id+"&status="+status,
		type: "PUT",
		dataType: "json",
		complete: function(XMLHttpRequest, textStatus, errorThrown) {
			if(textStatus == "error") {
				var errJson = eval("("+XMLHttpRequest.responseText+")");
				var errMsg = "操作失败，"+errJson.message;
				$("#errMsg").html(errMsg);
				alert(errMsg);
			}
			reloadTable();
		}
	});
}
</script>
<input type="hidden" id="commodityId" name="commodityId" value="${commodity.id}">
<input type="hidden" id="commodityName" name="commodityName" value="${commodity.name}">
<div id="errMsg" class="error">
	#{error 'errMsg'/}
</div>
<div class="list_criteria">
	<input type="hidden" id="currentPage" class="queryBean" name="queryBean.currentPage" value="1">
	每页
	<select class="queryBean" name="queryBean.quantityEachPage" onchange="reloadTable()">
		<option value="2">2</option>
		<option value="10" selected>10</option>
		<option value="25">25</option>
		<option value="50">50</option>
	</select>
	条记录&nbsp;&nbsp;
	按照
	<select class="queryBean" name="queryBean.orderby" onchange="reloadTable()">
		<option value="group.priority" selected>权重</option>
		<option value="price">价格</option>
		<option value="group.name">用户组</option>
		<option value="id">ID</option>
	</select>
	<select class="queryBean" name="queryBean.isDesc" onchange="reloadTable()">
		<option value="true" selected>降序</option>
		<option value="false">升序</option>
	</select>
	排序&nbsp;&nbsp;
</div>
<input type="button" value="新增" onClick="newPriceOpen()">
<div id="tableDiv"></div>
<div id="dialog-form" title="设置价格" style="font-size:1.4em;">
		<div id="errMsg" class="error">
		#{error 'savePriceMsg'/}
		</div>
		<br>
		<label for="editCommodityName">商&nbsp;&nbsp;&nbsp;&nbsp;品：</label>
		<input type="hidden" id="editCommodityId" name="pricedCommodity.commodity.id" >
		<input type="text" id="editCommodityName" name="pricedCommodity.commodity.name" readonly="readonly" id="name" class="text ui-widget-content ui-corner-all" />
		<br>
		<label for="editGroupId">用户组：</label>
		<select name="pricedCommodity.group.id" id="editGroupId">
			<option value=""></option>
			#{if userGroups}
			#{list items:userGroups, as:'userGroup'}
				<option value="${userGroup.id}">${userGroup.name}</option>
			#{/list}
			#{/if}
		</select>
		<br>
		<label for="editPricedCommodityPrice">价&nbsp;&nbsp;&nbsp;&nbsp;格：</label>
		<input type="text" name="pricedCommodity.price" id="editPricedCommodityPrice" value="" maxlength="8" class="text ui-widget-content ui-corner-all" />
</div>